/**
 * Created by guojianyu on 2015/9/11.
 */
myApp.config(function ($routeProvider) {
    $routeProvider.when('/user/view', {
        templateUrl: 'user/view',
        controller: 'userListController'
    })
});
myApp.service('userListService', function ($http) {
    var doRequest = function () {
        return $http.get('user/list');
    }
    return {
        userList: function () {
            return doRequest();
        }
    };
});
myApp.controller('userListController', function ($scope, $location, $http, userListService,Notify) {
    /*获取表单数据*/
    userListService.userList().success(function (response) {
        $scope.users = response;
    });
    /*删除用户*/
    $scope.deleteUser = function (id, name) {
        if (confirm('确定要删除"' + name + '"吗?')) {
            $http.get('user/delete/' + id).success(function () {
                Notify.success('删除成功');
                /*重新获取页面数据目的刷新refresh*/
                userListService.userList().success(function (response) {
                    $scope.users = response;
                });
            }).error(function () {
                Notify.error('删除失败');
            });
        }
    };
    /*增加用户*/
    $scope.addUser=function(){
        $scope.user={};
    };
    /*修改用户*/
    $scope.updateUser=function(data){
        $scope.user =angular.copy(data);
    };
    $scope.saveUser = function () {
        $http.post('user/edit', $scope.user).success(function () {
            Notify.success("success");
            /*重新获取页面数据目的刷新refresh*/
            userListService.userList().success(function (response) {
                $scope.users = response;
            });
        }).error(function () {
            Notify.error('失败');
        });
    };
});

